package com.zf.shuashua.ui.mine;

import android.content.Intent;
import android.text.TextUtils;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.ListVideo;
import com.zf.shuashua.bean.MoreMeVideoList;
import com.zf.shuashua.ui.custom.Divider2ItemDecoration;
import com.zf.shuashua.video.VideoPlayActivity;

import java.util.List;

import io.reactivex.Observable;

/**
 * A simple {@link Fragment} subclass.
 */
public class LikeVideoFragment extends BaseListFragment<ListVideo.DataBean> {

    private RecyclerView rv;
    private ListAdapter mAdapter;

    public static LikeVideoFragment getFragment() {
        return new LikeVideoFragment();
    }

    @Override
    protected void initView() {
        setEmptyLayout(R.layout.empty_feed);
        super.initView();
    }

    @Override
    protected void initData() {
        loadPage(1);
    }

    @Override
    protected RecyclerView getRecyclerView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(mActivity, 2));
        rv.addItemDecoration(new Divider2ItemDecoration(getActivity()));
        return rv;
    }

    @Override
    protected MeiBaseAdapter<ListVideo.DataBean> getAdapter() {
        mAdapter = new ListAdapter(R.layout.lxd_minevideo_item1, null);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MoreMeVideoList dataList = new MoreMeVideoList();
            dataList.setDataList(adapter.getData());
            Intent intent = new Intent(mActivity, VideoPlayActivity.class);
            intent.putExtra("position", position);
            intent.putExtra("moreMeList", dataList);
            mActivity.startActivity(intent);
        });
        return mAdapter;
    }

    @Override
    protected Observable<Result<List<ListVideo.DataBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getListVideo("", pageNo);
        return getListData(observable);
    }

    private Observable<Result<List<ListVideo.DataBean>>> getListData(Observable<String> observable) {
        final Result<List<ListVideo.DataBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    ListVideo likeVideo = ParseJsonUtils.parseData(s, ListVideo.class);
                    result.data = likeVideo.getData();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }


    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_like_video;
    }
}
